﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace RTS_RPG
{
    public class Area_Loader
    {
        public Area_TextureManager textures;
        public PlayerStats pstats;
        public String area_name;

        public Area_Loader(String _area_name, Area_TextureManager _textures, PlayerStats _pstats)
        {
            area_name = _area_name;
            textures = _textures;
            pstats = _pstats;
        }

        // For buildings or other entries to areas within areas
        public List<Site> find_area_entries(IServiceProvider services)
        {
            List<Site> places = new List<Site>();
            switch (area_name)
            {
                case "test_area":
                    List<Area> areas = new List<Area>();
                    Vector2 temp = new Vector2(600, 300);
                    Vector2 NaN = new Vector2(Program.NaN, Program.NaN);

                    List<Vector2> coords = new List<Vector2>();
                    coords.Add(temp);
                    areas.Add(new Area("Building", new GameMap(null, NaN, null), coords, pstats, services));

                    temp.X = 200;
                    temp.Y = 200;
                    places.Add(new Site("Building", textures.keys_up, areas[0], temp));
                    break;
                default:
                    break;
            }
            return places;
        }

        // Finds NPCs for this area
        public List<NPC> find_area_npcs()
        {
            List<NPC> npcs = new List<NPC>();
            switch (area_name)
            {
                case "test_area":
                    Vector2 temp = new Vector2(500, 500);
                    string speech = "I am the first NPC!";

                    npcs.Add(new NPC(textures.character_main, temp, "Bob", speech));
                    break;
                default:
                    break;
            }
            return npcs;
        }

        public GameMap get_game_map()
        {
            Vector2 NaN = new Vector2(Program.NaN, Program.NaN);
            GameMap result = new GameMap(null, NaN, null);
            List<Block> blocks = new List<Block>();
            StreamReader reader = new StreamReader(area_name + ".txt");
            string line = "";
            int y = 0;
            while (true)
            {
                line = reader.ReadLine();
                if (line == null) break;
                for (int x = 0; x < line.Length; ++x)
                {
                    if (line[x] == 'X') 
                        blocks.Add(new Block(textures.block_a, new Vector2(x * 50, y), true));
                }
                y += 50;
            }
            result = new GameMap(null, new Vector2(Program.SCREENWIDTH, Program.SCREENHEIGHT), blocks);
            return result;
        }
    }
}
